package com.wandian.assets
{
	import flash.display.*;
	import flash.events.*;
	import flash.media.*;
	import flash.system.*;

	public class AssetsLibrary extends EventDispatcher
	{
		private var fLibraries_sl_arr:Array;
		private var fExternAssetsLibrary_obj:Object;
		public static const EVENT_ASSETS_LIBRARY_SOUNDS_LEVEL_UPDATED:String="onAssetsLibraryVideoLevelUpdated";
		public static const EVENT_ASSETS_LIBRARY_VIDEO_LEVEL_UPDATED:String="onAssetsLibraryVideoLevelUpdated";
		public static const EVENT_ASSETS_LIBRARY_MODIFIED:String="onAssetsLibraryStructureModified";
		public static const EVENT_ASSETS_LIBRARY_BASE_LEVEL_UPDATED:String="onAssetsLibraryBaseLevelUpdated";
		private static var fAssetsLibraryRef_al:AssetsLibrary;
		public static const EVENT_ASSETS_LIBRARY_TRANSLATION_LEVEL_UPDATED:String="onAssetsLibraryTranslationLevelUpdated";

		public function AssetsLibrary()
		{
			if (fAssetsLibraryRef_al)
			{
				throw Error("Instance already exists! Use AssetsLibrary.getInstance() instead of new AssetsLibrary()!");
			}
			fAssetsLibraryRef_al=this;
			return;
		} // end function

		public function newSoundFromLibraryLevel(param1, param2:String, param3:Boolean=true, param4:String="anyLibraryId"):Sound
		{
			if (this.externAssetsLibrary)
			{
				return this.externAssetsLibrary.newSoundFromLibraryLevel(param1, param2, param3, param4);
			}
			return Sound(this.newObjectFromLibraryLevel(param1, param2, param3, param4));
		} // end function

		protected function onTranslationLevelUpdated():void
		{
			this.dispatchEvent(new Event(EVENT_ASSETS_LIBRARY_TRANSLATION_LEVEL_UPDATED));
			this.onAssetsLibraryModified();
			return;
		} // end function

		public function newMovieClipFromLibraryLevel(param1, param2:String, param3:Boolean=true, param4:String="anyLibraryId"):MovieClip
		{
			if (this.externAssetsLibrary)
			{
				return this.externAssetsLibrary.newMovieClipFromLibraryLevel(param1, param2, param3, param4);
			}
			return MovieClip(this.newObjectFromLibraryLevel(param1, param2, param3, param4));
		} // end function

		public function newSound(param1:String, param2:Boolean=true, param3:String="anyLibraryId"):Sound
		{
			var _loc_4:Sound=null;
			if (this.externAssetsLibrary)
			{
				return this.externAssetsLibrary.newSound(param1, param2, param3);
			}
			_loc_4=this.newSoundFromLibraryLevel(param1, SubLibrary.LIBRARY_LEVEL_TRANSLATION, false, param3);
			if (_loc_4)
			{
				return _loc_4;
			}
			_loc_4=this.newSoundFromLibraryLevel(param1, SubLibrary.LIBRARY_LEVEL_SOUNDS, false, param3);
			if (_loc_4)
			{
				return _loc_4;
			}
			_loc_4=this.newSoundFromLibraryLevel(param1, SubLibrary.LIBRARY_LEVEL_BASE, false, param3);
			if (_loc_4)
			{
				return _loc_4;
			}
			if (param2)
			{
				throw Error("No sound in the assets library: " + param1 + ", " + param3);
			}
			return null;
		} // end function

		public function removeLibrary(param1:ApplicationDomain, param2:String="baseLibraryLevel", param3:Boolean=true, param4:Boolean=true, param5:String="undifinedLibraryId"):void
		{
			var _loc_6:int=0;
			_loc_6=this.getLibraryOffset(param1, param2, param3, param5);
			if (_loc_6 == -1)
			{
				return;
			}
			this.libraries.splice(_loc_6, 1);
			if (param4)
			{
				this.onLibraryLevelUpdated(param2);
			}
			return;
		} // end function

		protected function get libraries():Array
		{
			if (!this.fLibraries_sl_arr)
			{
				this.fLibraries_sl_arr=new Array();
			}
			return this.fLibraries_sl_arr;
		} // end function

		public function addLibrary(param1:ApplicationDomain, param2:String="baseLibraryLevel", param3:Boolean=true, param4:String="undifinedLibraryId"):void
		{
			if (this.externAssetsLibrary)
			{
				this.externAssetsLibrary.addLibrary(param1, param2, param3, param4);
				return;
			}
			if (this.libraryExists(param1, param2, param4))
			{
				this.removeLibrary(param1, param2, true, false);
			}
			this.libraries.push(new SubLibrary(param1, param2, param4));
			if (param3)
			{
				this.onLibraryLevelUpdated(param2);
			}
			return;
		} // end function

		protected function clearAll():void
		{
			this.fLibraries_sl_arr=new Array();
			return;
		} // end function

		protected function onBaseLevelUpdated():void
		{
			this.dispatchEvent(new Event(EVENT_ASSETS_LIBRARY_BASE_LEVEL_UPDATED));
			this.onAssetsLibraryModified();
			return;
		} // end function

		public function newMovieClip(param1:String, param2:Boolean=true, param3:String="anyLibraryLevel", param4:String="anyLibraryLevel"):MovieClip
		{
			var _loc_5:MovieClip=null;
			if (this.externAssetsLibrary)
			{
				return this.externAssetsLibrary.newMovieClip(param1, param2, param3, param4);
			}
			if (param3 != SubLibrary.LIBRARY_LEVEL_ANY)
			{
				_loc_5=this.newMovieClipFromLibraryLevel(param1, param3, false, param4);
				if (_loc_5)
				{
					return _loc_5;
				}
			}
			else
			{
				_loc_5=this.newMovieClipFromLibraryLevel(param1, SubLibrary.LIBRARY_LEVEL_TRANSLATION, false, param4);
				if (_loc_5)
				{
					return _loc_5;
				}
				_loc_5=this.newMovieClipFromLibraryLevel(param1, SubLibrary.LIBRARY_LEVEL_VIDEO, false, param4);
				if (_loc_5)
				{
					return _loc_5;
				}
				_loc_5=this.newMovieClipFromLibraryLevel(param1, SubLibrary.LIBRARY_LEVEL_BASE, false, param4);
				if (_loc_5)
				{
					return _loc_5;
				}
			}
			if (param2)
			{
				throw Error("No MC in the assets library: " + param1 + ", " + param3 + ", " + param4);
			}
			return null;
		} // end function

		protected function onVideoLevelUpdated():void
		{
			this.dispatchEvent(new Event(EVENT_ASSETS_LIBRARY_VIDEO_LEVEL_UPDATED));
			this.onAssetsLibraryModified();
			return;
		} // end function

		protected function onAssetsLibraryModified():void
		{
			trace("AssetsLibrary.onAssetsLibraryModified() called");
			this.dispatchEvent(new Event(EVENT_ASSETS_LIBRARY_MODIFIED));
			return;
		} // end function

		protected function onLibraryLevelUpdated(param1:String):void
		{
			switch (param1)
			{
				case SubLibrary.LIBRARY_LEVEL_BASE:
				{
					this.onBaseLevelUpdated();
					break;
				}
				case SubLibrary.LIBRARY_LEVEL_TRANSLATION:
				{
					this.onVideoLevelUpdated();
					break;
				}
				case SubLibrary.LIBRARY_LEVEL_VIDEO:
				{
					this.onTranslationLevelUpdated();
					break;
				}
				case SubLibrary.LIBRARY_LEVEL_SOUNDS:
				{
					this.onSoundsLevelUpdated();
					break;
				}
				default:
				{
					throw Error("Unknown library level: " + param1);
					break;
				}
			}
			return;
		} // end function

		public function set externAssetsLibrary(param1:Object):void
		{
			if (this.externAssetsLibrary)
			{
				throw Error("Extern assets library already defined!");
			}
			if (Object(this) == Object(param1))
			{
				return;
			}
			this.fExternAssetsLibrary_obj=param1;
			this.externAssetsLibrary.addEventListener(EVENT_ASSETS_LIBRARY_BASE_LEVEL_UPDATED, this.dispatchEvent);
			this.externAssetsLibrary.addEventListener(EVENT_ASSETS_LIBRARY_VIDEO_LEVEL_UPDATED, this.dispatchEvent);
			this.externAssetsLibrary.addEventListener(EVENT_ASSETS_LIBRARY_SOUNDS_LEVEL_UPDATED, this.dispatchEvent);
			this.externAssetsLibrary.addEventListener(EVENT_ASSETS_LIBRARY_TRANSLATION_LEVEL_UPDATED, this.dispatchEvent);
			this.externAssetsLibrary.addEventListener(EVENT_ASSETS_LIBRARY_MODIFIED, this.dispatchEvent);
			return;
		} // end function

		public function newObjectFromLibraryLevel(param1, param2:String, param3:Boolean=true, param4:String="anyLibraryId"):Object
		{
			var _loc_5:Object=null;
			var _loc_6:int=0;
			var _loc_7:SubLibrary=null;
			if (this.externAssetsLibrary)
			{
				return this.externAssetsLibrary.newObjectFromLibraryLevel(param1, param2, param3, param4);
			}
			_loc_5=null;
			_loc_6=this.libraries.length - 1;
			while (_loc_6 >= 0)
			{

				_loc_7=this.libraries[_loc_6];
				_loc_5=_loc_7.newObject(param1, false, param2, param4);
				if (_loc_5)
				{
					return _loc_5;
				}
				_loc_6=_loc_6 - 1;
			}
			if (param3)
			{
				throw Error("No object in the assets library level: " + param1 + ", " + param2 + ", " + param4);
			}
			return null;
		} // end function

		protected function clearLibraryLevel(param1:String, param2:Boolean=true):void
		{
			var _loc_3:Array=null;
			var _loc_4:int=0;
			var _loc_5:SubLibrary=null;
			_loc_3=new Array();
			_loc_4=0;
			while (_loc_4 < this.libraries.length)
			{

				_loc_5=this.libraries[_loc_4];
				if (_loc_5.libraryLevel == param1)
				{
				}
				else
				{
					_loc_3.push(_loc_5);
				}
				_loc_4++;
			}
			this.fLibraries_sl_arr=_loc_3;
			if (param2)
			{
				this.onLibraryLevelUpdated(param1);
			}
			return;
		} // end function

		protected function libraryExists(param1:ApplicationDomain, param2:String, param3="undifinedLibraryId"):Boolean
		{
			if (this.getLibraryOffset(param1, param2, false, param3) != -1)
			{
				return true;
			}
			return false;
		} // end function

		public function get externAssetsLibrary():Object
		{
			return this.fExternAssetsLibrary_obj;
		} // end function

		protected function onSoundsLevelUpdated():void
		{
			this.dispatchEvent(new Event(EVENT_ASSETS_LIBRARY_SOUNDS_LEVEL_UPDATED));
			this.onAssetsLibraryModified();
			return;
		} // end function

		protected function getLibraryOffset(param1:ApplicationDomain, param2:String, param3:Boolean=true, param4:String="undifinedLibraryId"):int
		{
			var _loc_5:int=0;
			var _loc_6:SubLibrary=null;
			_loc_5=0;
			while (_loc_5 < this.libraries.length)
			{

				_loc_6=this.libraries[_loc_5];
				if (_loc_6.hasProperties(param2, param4) && _loc_6.applicationDomain == param1)
				{
					return _loc_5;
				}
				_loc_5++;
			}
			if (param3)
			{
				throw Error("Library does not exist on the level: " + param2);
			}
			return -1;
		} // end function

		public static function getInstance():AssetsLibrary
		{
			if (!fAssetsLibraryRef_al)
			{
				new AssetsLibrary;
			}
			return fAssetsLibraryRef_al;
		} // end function

	}
}
